/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: orbis_mnc_agg.do
Date: October 2022

Description: orbis_aggregates

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"
*--------------------------------------------------------



clear all
set more off
local var0 sales 
local varset sales emp exports va labcost 
local type "naics"

use year isocode id_bvd guo_bvd hq `varset' sector* NAICS* con_code using "${data}/firm_allyears_sales.dta", clear 
drop if `var0'==. | `var0'==0
tab con_code
drop if con_code=="$lf"
drop con_code
tab sector

*Choosing the industry level 
*-----------------------------------------------------
if "`type'"=="naics" {
drop if sector=="NA"
tab sector
local industry sector 
}

*Collapse at the level of the parent-industry-isocode (this includes the sales of the GUO-isocode at home as well as in other countries)
*-----------------------------------------------------
sort year isocode sector1 sector `industry' hq guo_bvd
collapse (sum) `varset', by(year isocode sector1 sector `industry' hq guo_bvd)


*Keep only MNCs in the sample based on the number of countries (across ALL SECTORS) 
*----------------------------------------------------- 
by year guo_bvd isocode, sort: gen a=_n==1
tab a
by year guo_bvd, sort: egen num_iso=total(a)
drop a

gen MNC=0
replace MNC=1 if num_iso>1
tab MNC
tab MNC if isocode!=hq
drop if MNC==0 & isocode==hq 
*Notice that The above line keeps firms with only one affiliate but in a foreign country.*/
tab MNC
drop if MNC==0 
*Nonetheless for identification an MNCs need to be in at least 2 foreign countries to be in the sample*/

keep if isocode=="DK" | isocode=="JP" | isocode=="IT" | isocode=="KR" | isocode=="DE" | isocode=="FR" | isocode=="ES" |  isocode=="MX" |  isocode=="GB" |  isocode=="PL" |  isocode=="GR" |  isocode=="NL" |  isocode=="RO" |  isocode=="BE" |  isocode=="AT" |  isocode=="PT" |  isocode=="FI" |  isocode=="SE" |  isocode=="CZ" |  isocode=="SK" |  isocode=="BG" |  isocode=="HU" |  isocode=="HR" |  isocode=="LV" |  isocode=="SI" |  isocode=="LT" |  isocode=="EE" 

keep if year==2016
drop if sales==0 | sales==.

by year isocode guo_bvd, sort: gen a=_n==1



gen affiliate=(isocode!=hq)
gen parent=(isocode==hq)

collapse (sum) affiliate parent, by(isocode sector1)


*---------------------------------------------------------------------
clear all
use "${data}/sin_firmlevel_naics_gravityLF.dta", clear
*use "${data}/reg0_naics_sales_s1_base_withparent_2016.dta", clear
keep if isocode=="DK" | isocode=="JP" | isocode=="IT" | isocode=="KR" | isocode=="DE" | isocode=="FR" | isocode=="ES" |  isocode=="MX" |  isocode=="GB" |  isocode=="PL" |  isocode=="GR" |  isocode=="NL" |  isocode=="RO" |  isocode=="BE" |  isocode=="AT" |  isocode=="PT" |  isocode=="FI" |  isocode=="SE" |  isocode=="CZ" |  isocode=="SK" |  isocode=="BG" |  isocode=="HU" |  isocode=="HR" |  isocode=="LV" |  isocode=="SI" |  isocode=="LT" |  isocode=="EE" 

keep if year==2016
drop if sin_sales==0 | sin_sales==.
drop if hq=="WW" | hq=="ZZ" | hq=="YY"
*drop if hq=="KY" | hq=="VG" |  hq=="LU" |  hq=="CH"
drop if sector1=="Non-Market Economy"


*Keep only MNCs in the sample based on the number of countries (across ALL SECTORS) 
*----------------------------------------------------- 
by year guo_bvd isocode, sort: gen a=_n==1
tab a
by year guo_bvd, sort: egen num_iso=total(a)
drop a

gen MNC=0
replace MNC=1 if num_iso>1
tab MNC
tab MNC if isocode!=hq
drop if MNC==0 & isocode==hq 
tab MNC
drop if MNC==0 

drop if hq==isocode
sort isocode sector1 sector

collapse (count) sin_sales sin_emp sin_va, by(year isocode sector1)

gen sec=""
replace sec="_manuf" if sector1=="Manufacturing (C)"
replace sec="_serv" if sector1=="Market_Services (G-H-I-J-K-M-N-R-S-T)"
replace sec="_other" if sector1=="Other_Goods (A-B-D-E-F)"
drop sector1 year 

reshape wide sin_sales sin_emp sin_va , i( isocode ) j(sec) string

foreach vv in sales emp va {
display "`vv'"
gen sin_`vv'_tot= sin_`vv'_manuf+ sin_`vv'_serv+ sin_`vv'_other
drop sin_`vv'_other
replace sin_`vv'_tot=. if sin_`vv'_tot==0
replace sin_`vv'_manuf=. if sin_`vv'_manuf==0
replace sin_`vv'_serv=. if sin_`vv'_serv==0
}
order isocode sin_sales_tot sin_sales_manuf sin_sales_serv sin_emp_tot sin_emp_manuf sin_emp_serv sin_va_tot sin_va_manuf sin_va_serv
format %9.0fc sin*


*---------------------------------------------------------



/*


*use "${data}/sin_firmlevel_naics_gravityLF.dta", clear



etime, start 
use "${data}/firm_allyears_sales.dta", clear
keep if isocode=="DK" | isocode=="JP" | isocode=="IT" | isocode=="KR" | isocode=="DE" | isocode=="FR" | isocode=="ES" |  isocode=="MX" |  isocode=="GB" |  isocode=="PL" |  isocode=="GR" |  isocode=="NL" |  isocode=="RO" |  isocode=="BE" |  isocode=="AT" |  isocode=="PT" |  isocode=="FI" |  isocode=="SE" |  isocode=="CZ" |  isocode=="SK" |  isocode=="BG" |  isocode=="HU" |  isocode=="HR" |  isocode=="LV" |  isocode=="SI" |  isocode=="LT" |  isocode=="EE" 

gen iso2=substr( id_bvd ,1,2)
gen hs2=substr( guo_bvd,1,2)
tab sector1
drop if hq==isocode
drop if hs2==iso2

drop if hq=="WW" | hq=="ZZ" | hq=="YY"
drop if hq=="KY" | hq=="VG" |  hq=="LU"  |  hq=="CH"
tab con_code
drop if sector1=="Non-Market Economy"

*Inspecting what is happening  
keep year isocode sector sector1 sales emp va exports con_code
collapse (sum) sales emp va, by(isocode year con_code sector sector1)
collapse (sum) sales emp va, by(isocode year con_code)

keep if (year==2016 & isocode!="MX") | ( year==2014 & isocode=="MX")
replace year=2016
sort isocode con_code

by isocode, sort: egen tot=total(sales)
gen share=sales/tot
table isocode con_, c(mean share)
tab con_code
drop if con_code=="LF" & isocode!="JP"
drop if con_code=="U2" & isocode!="MX"
collapse (sum) sales emp va, by(isocode year)


